/********************************************************************
Name: Figure 4: Smaller Partisan Differences Among Sheriffs 
	Than Public (Right Panel)
Author: Dan Thompson
Date: August 2019
Notes:
********************************************************************/

* Set the working directory 
gl root = "$sanctuary_root"
gl root = "YOUR_WORKING_DIRECTORY"

* Bring in the CCES file and estimate the dem v rep differences
// note: weighting so that each county is one unit so it matches 
// the weighting of sheriffs (i.e., 1 per county)
use "$root/data/cces immigration attitude analysis data.dta", clear
keep if year==2012 & in_sheriff==1
gen b_diff = .
gen upper_diff = .
gen lower_diff = .
gen outcome = ""
local i = 1
foreach y in "for_questioning" "for_more_patrol" {
	xtreg `y' dem if (dem==1|rep==1) & in_sheriff_survey==1 [aw=weight1], fe i(fips) robust
	replace b_diff = _b[dem]*100 if _n==`i'
	replace upper_diff = _b[dem]*100 + 1.96*_se[dem]*100 if _n==`i'
	replace lower_diff = _b[dem]*100 - 1.96*_se[dem]*100 if _n==`i'
	replace outcome = "`y'" if _n==`i'
	local i = `i' + 1
}
gen var = 1 if outcome=="for_more_patrol"
replace var = 3 if outcome=="for_questioning"
keep var b_diff upper_diff lower_diff
tempfile cces
save `cces'

* Bring in the sheriff survey file
use "$root/data/sheriff survey - cleaned.dta", clear

* Define the containers for analysis output
gen var = .
gen b_diff = .
gen upper_diff = .
gen lower_diff = .

* Run the regressions
local i = 1
foreach v in "spend" "inquire" {
	replace var = `i' if  _n==`i'
	reg bin_`v' dem if bin_s!=. & bin_in!=. & bin_ir!=. & bin_a!=., r
	replace b_diff = _b[dem] if _n==`i'
	replace upper_diff = _b[dem] + 1.96*_se[dem] if _n==`i'
	replace lower_diff = _b[dem] - 1.96*_se[dem] if _n==`i'
	local ++i
}

* Label the survey variables
label define var_lab 1 "Spending" 2 "Inquire"
label value var var_lab

* Get rid of the unnecessary columns and rows
keep var b_diff upper_diff lower_diff
keep if var!=.
keep if var<3
replace var = 4 if var==2
replace var = 2 if var==1
append using `cces'

* Rescale the estimates to range from 0 to 1
replace b_diff = b_diff/100
replace upper_diff = upper_diff/100
replace lower_diff = lower_diff/100

* Label the survey variables
label drop var_lab
label define var_lab 1 "Border" 2 "Border" ///
	3 "Inquire" 4 "Inquire"
label value var var_lab

* Plot the difference between Dem and Rep responses
twoway (rcap upper_diff lower_diff var if inlist(var,1,3), lc(gs2)) ///
	(scatter b_diff var if inlist(var,1,3), mc(gs2)) ///
	(rcap upper_diff lower_diff var if inlist(var,2,4), lc(gs8)) ///
	(scatter b_diff var if inlist(var,2,4), mc(gs8)), ///
	yline(0, lc(gs12) lp(dash)) ///
	ylab(, labs(medium)) ///
	xlabel(1.5 "More Patrols on Border" 3.5 "Ask About Citizenship", ///
		valuelabel notick) xt("") xsc(r(0.5 4.5)) ///
	yti("Dem - Rep Share in Agreement", si(medlarge)) ///
	bgcolor(white) graphregion(color(white)) ///
	legend(order(2 "Public" 4 "Sheriffs")) ///
	name("sheriff_cces", replace)
